<template>
  <div>
    <el-card>
      <el-button type="primary" @click="addExchange()">添加兑换码</el-button>
      <!-- <el-col :span="4">
          <el-select v-model="status">
            <el-option :label="1">未使用</el-option>
            <el-option :label="2">已使用</el-option>
          </el-select>
      </el-col>-->
      <el-row style="margin-top:20px;">
        <el-col :span="6">
          <el-input placeholder="请输入兑换码名称" v-model="searchCont"></el-input>
        </el-col>
        <el-col :span="4" :offset="1">
          <el-button type="primary" @click="getData(1)">搜索</el-button>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showDesc = true">添加兑换码说明</el-button>
        </el-col>
      </el-row>

      <el-table :data="exchangeList">
        <el-table-column label="激活码名称" prop="rechargeName" align="center"></el-table-column>

        <el-table-column label="关联代理人" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.nickName == '' ?'无关联人' :scope.row.nickName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="已兑换/未兑换/发放总数" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.used}}</span> /
            <span>{{scope.row.enableShare}}</span> /
            <span>{{scope.row.num}}</span>
          </template>
        </el-table-column>
        <el-table-column label="剩余卡券" align="center">
          <template slot-scope="scope">
            <span>{{(Number(scope.row.num)*10000-Number(scope.row.used)*10000)/10000}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="goExchangeStock(scope.row.rechargeId,scope.row.enableShare)"
            >兑换码库</el-button>
            <el-button
              v-if="scope.row.status == 1"
              type="warning"
              size="mini"
              @click="toStopRow(scope.row.rechargeId,0)"
            >停用</el-button>
            <el-button
              v-if="scope.row.status == 0"
              type="success"
              @click="toStopRow(scope.row.rechargeId,1)"
              size="mini"
            >启用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="currentPage"
        style="text-align:center;margin-top:20px;"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog :visible.sync="showAddExchanage" title="添加兑换码">
      <el-form
        :model="addTicktForm"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
        class="demo-ruleForm"
      >
        <el-form-item label="兑换码名称" prop="name">
          <el-input style="width:50%;" v-model="addTicktForm.name"></el-input>
        </el-form-item>
        <!-- <label style="margin-left:59px">
          <font color="red">*</font> 兑换码类型：
        </label>-->
        <!-- <el-button type="primary" size="small" @click="vipShow = !vipShow">VIP兑换码</el-button> -->
        <!-- <el-button type="primary" size="small" @click="OBShow = !OBShow">欧币兑换码</el-button> -->
        <p>VIP兑换码</p>
        <section>
          <el-form-item label="VIP兑换码：">
            <el-row>
              <el-col :span="16">
                <el-table :data="vipList" style="width:370px">
                  <el-table-column label="添加数量" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.num"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="VIP类型" align="cneter">
                    <template slot-scope="scope">
                      <el-select v-model="scope.row.type" placeholder="请选择">
                        <el-option :key="1" label="黄金会员" :value="1">黄金会员</el-option>
                        <el-option :key="3" label="钻石会员" :value="3">钻石会员</el-option>
                        <el-option :key="4" label="至尊会员" :value="4">至尊会员</el-option>
                      </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="VIP周期(月)" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.period"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                  <!-- <el-table-column label="方案包个数" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.giveCombo"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>-->
                </el-table>
              </el-col>
              <el-col :span="3">
                <el-button @click="clickAdd(1)" size="mini" type="success">添加</el-button>
              </el-col>
              <el-col :span="3" :offset="1">
                <el-button @click="clickDelete(1)" size="mini" type="danger">删除</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </section>

        <section v-if="OBShow">
          <el-form-item label="欧币兑换码：">
            <el-row>
              <el-col :span="16">
                <el-table :data="OBList" style="width:370px">
                  <el-table-column label="添加数量" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.num"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="欧币金额" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.money"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
              <el-col :span="3">
                <el-button @click="clickAdd(2)" size="mini" type="success">添加</el-button>
              </el-col>
              <el-col :span="3" :offset="1">
                <el-button @click="clickDelete(2)" size="mini" type="danger">删除</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </section>

        <el-form-item label="是否关联代理商" prop="agent">
          <el-radio-group v-model="addTicktForm.agent">
            <el-radio label="是"></el-radio>
            <el-radio label="否"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="addTicktForm.agent == '是'" label="选择合伙人" prop="agent">
          <el-select v-model="addTicktForm.agenterId">
            <el-option
              v-for="item in agenterList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >{{item.vipType == 3 ?'合伙人' :item.vipType == 5 ?'运营中心' :item.vipType == 6 ?'分公司' :''}} {{item.nickName}}</el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddExchanage = false">取 消</el-button>
        <el-button type="primary" @click="addData()">添 加</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="showDesc" title="兑换码说明编辑">
      <el-form>
        <el-form-item label="兑换码说明:">
          <el-input type="textarea" v-model="exchangeCodeExplan"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddExchanage = false">取 消</el-button>
        <el-button type="primary" @click="saveExplan()">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  activateList,
  activate,
  activateStatus,
  activateExplain
} from "@/api/member";
export default {
  data() {
    return {
      exchangeList: [],
      exchangeCodeExplan: "",
      showAddExchanage: false,
      addTicktForm: {
        agent: "否",
        name: "",
        agenterId: "",
        giveCombo: ""
      },
      searchCont: "",
      rules: {
        name: [{ required: true, message: "请输入兑换码名称", trigger: "blur" }]
      },
      vipList: [{ num: 0, period: 0, type: 1 }],
      OBList: [{ num: 0, money: 0, type: 2 }],
      OBShow: false,
      agenterList: [],
      exchangeData: [],
      total: 0,
      currentPage: 0,
      showDesc: false
    };
  },
  watch: {
    addTicktForm(val) {
      console.log(val);
    },
    currentPage(val) {
      this.getData(val);
    }
  },
  methods: {
    saveExplan() {
      activateExplain({ explain: this.exchangeCodeExplan }).then(res => {
        this.$message(res.data.data);
      });
    },
    toStopRow(id, val) {
      activateStatus({ rechargeId: id, status: val }).then(res => {
        if (val == 0) {
          this.$message("已停用");
        } else {
          this.$message("已启用");
        }
        this.getData(1);
      });
    },
    addData() {
      if (this.addTicktForm.name == "") {
        this.$message("请填写兑换码名称");
        return;
      }
      // if (this.vipShow && this.OBShow) {
      //   this.exchangeData = this.vipList.concat(this.OBList);
      // } else if (this.vipShow) {
      this.exchangeData = this.vipList;
      // } else if (this.OBShow) {
      //   this.exchangeData = this.OBList;
      // } else if (!this.vipShow) {
      //   this.$message("请选择兑换码类型");
      //   return;
      // }
      let data = {
        name: this.addTicktForm.name,
        relevanceId: this.addTicktForm.agenterId,
        // giveCombo:this.addTicktForm.giveCombo,
        exchangeData: this.exchangeData
      };
      console.log(data);

      activate(data).then(res => {
        console.log(res);
        this.$message(res.data.data);
      });
      this.addTicktForm = {
        agent: "否",
        name: "",
        agenterId: ""
      };
      this.vipList = [];
      this.OBList = [];
      this.showAddExchanage = false;
      this.getData(1);
    },
    clickDelete(val) {
      if (val == 1) {
        if (this.vipList.length > 1) {
          this.vipList.pop();
        }
        return;
      } else {
        if (this.OBList.length > 1) {
          this.OBList.pop();
        }
      }
    },
    getData(page) {
      let data = {
        page: page,
        name: this.searchCont
      };
      activateList(data).then(res => {
        console.log(res);
        this.exchangeList = res.data.data.data.data;
        this.agenterList = res.data.data.user;
        this.exchangeCodeExplan = res.data.data.explain.explain;
        this.total = res.data.data.data.total;
      });
    },
    addExchange() {
      this.showAddExchanage = true;
    },
    goExchangeStock(id, unUse) {
      this.$router.push({
        path: "exchangeStock",
        query: {
          rechargeId: id
        }
      });
    },
    clickAdd(val) {
      if (val == 1) {
        this.vipList.push({ num: 0, period: 0, type: 1 });
      } else {
        this.OBList.push({ num: 0, money: 0, type: 2 });
      }
    }
  },
  mounted() {
    this.getData(1);
  }
};
</script>
